package com.chushouya.order.dao.repository;


import com.general.framework.core.lang.Integers;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.order.dao.entity.OrderLogisticsEntity;
import com.chushouya.order.dao.mapper.OrderLogisticsEntityMapper;
import com.chushouya.order.dto.admin.orderlogistics.OrderLogisticsQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Slf4j
@Repository
public class OrderLogisticsRepository extends BaseRepository<OrderLogisticsEntityMapper, OrderLogisticsEntity, Long> {


    public OrderLogisticsEntity selectOne(OrderLogisticsQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    private Example buildExample(OrderLogisticsQuery query) {
        Example example = buildExample();
        if (Longs.notNullAndZero(query.getOrderId())) {
            example.and().andEqualTo("orderId", query.getOrderId());
        }
        if (Strings.isNotEmpty(query.getExpressNo())) {
            example.and().andEqualTo("expressNo", query.getExpressNo());
        }
        if (Objects.nonNull(query.getType())) {
            example.and().andEqualTo("type", query.getType());
        }

        example.orderBy("orderLogisticsId").desc();
        return example;
    }

    public List<OrderLogisticsEntity> selectList(OrderLogisticsQuery query) {
        return this.selectByExample(this.buildExample(query));
    }


}
